//当前项目的路由模块

//1.导入Vue和VueRouter的包
import Vue from "vue"
import VueRouter from "vue-router"
import pathArr from "@/router/pathArr.js";
//导入需要的组件
import Login from "@/components/MyLogin";
import Home from "@/components/MyHome";
import Users from "@/components/menus/MyUsers";
import Rights from "@/components/menus/MyRights";
import Goods from "@/components/menus/MyGoods";
import Settings from "@/components/menus/MySettings";
import Orders from "@/components/menus/MyOrders";
import UserDetail from "@/components/user/MyUserDetail";



//2.调用Vue.use()函数，把VueRouter安装为Vue插件
Vue.use(VueRouter)

//3.创建路由的实例对象
const router = new VueRouter({
    routes: [
        {path: '/',redirect: '/login'},
        //登录路由规则
        {path: '/login',component: Login},
        //后台主页路由规则
        {path: '/home',component: Home,redirect: '/home/users',children: [
                {path: 'users',component: Users},
                {path: 'rights',component: Rights},
                {path: 'goods',component: Goods},
                {path: 'settings',component: Settings},
                {path: 'orders',component: Orders},
                //用户详情页
                {path: 'detail/:id',component: UserDetail}
            ]}
    ]
})
router.beforeEach(function (to,f,next){
    if (pathArr.indexOf(to.path) !== -1){
        const token = localStorage.getItem('token')
        if (token){
            next()
        }else {
            next('/login')
        }
    }else {
        next()
    }
})
//4.向外共享路由的实例对象
export default router